package com.yl.ydk.utils

import android.app.Activity
import android.content.Context
import android.os.Build
import android.telecom.Call
import android.text.InputType
import android.view.WindowManager
import android.widget.EditText
import com.blankj.utilcode.utils.EncryptUtils
import okhttp3.FormBody
import okhttp3.RequestBody
import java.lang.reflect.Method
import java.util.*


object CommontUtils {


    /**
     * 禁止Edittext弹出软件盘
     */
    fun disableShowSoftInput(editText: EditText, context: Activity) {
        if (android.os.Build.VERSION.SDK_INT <= 10) {
            editText.inputType = InputType.TYPE_NULL;
        } else {//禁止Edittext弹出软件盘
            //Call是当前活动
            context.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
            try {//光标依然正常显示
                val cls: Class<EditText> = EditText::class.java
                val method: Method =
                    cls.getMethod("setSoftInputShownOnFocus", Boolean::class.javaPrimitiveType)
                method.isAccessible = true
                method.invoke(editText, false)
            } catch (e: Exception) {
                e.printStackTrace();
            }
        }
    }

    fun getRandomString(length: Int): String? {
        val str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        val random = Random()
        val sb = StringBuffer()
        for (i in 0 until length) {
            val number: Int = random.nextInt(62)
            sb.append(str[number])
        }
        return sb.toString()
    }

    fun getToken(): String {
        val key = "&key=42815847aa54d62afe7efe4b27fb3b01"
        return EncryptUtils.encryptMD5ToString(key).toUpperCase()
    }

    fun getPrama(type: Int, page: Int): RequestBody? {
        val key = "category_two=$type&page=$page&key=42815847aa54d62afe7efe4b27fb3b01"
        val token = EncryptUtils.encryptMD5ToString(key).toUpperCase()
        return FormBody.Builder()
            .add("category_two", type.toString())
            .add("page", page.toString())
            .add("_token", token)
            .build()
    }

    fun getHealthtestQuestionPrama(query: Int?): RequestBody? {
        val key = "e_id=$query&key=42815847aa54d62afe7efe4b27fb3b01"
        val token = EncryptUtils.encryptMD5ToString(key).toUpperCase()
        return FormBody.Builder()
            .add("e_id", query.toString())
            .add("_token", token)
            .build()
    }

    fun getTestFinishPrama(e_id: Int, total_score: Int?, user_id: String): RequestBody? {
        val key = "e_id=$e_id&total_score=$total_score&key=42815847aa54d62afe7efe4b27fb3b01"
        val token = EncryptUtils.encryptMD5ToString(key).toUpperCase()
        return FormBody.Builder()
            .add("e_id", e_id.toString())
            .add("total_score", total_score.toString())
            .add("user_id", user_id)
            .add("_token", token)
            .build()
    }

}